package com.shareinfo.auto.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.shareinfo.auto.dao.TableDao;
import com.shareinfo.auto.service.TableService;
import com.shareinfo.auto.vo.ColumnInfo;
import com.shareinfo.auto.vo.ConfigureInit;
import com.shareinfo.auto.vo.ResultInfo;
import com.shareinfo.auto.vo.TableInfo;

/**
 * 
 * @author Administrator
 *
 */
@Service("tableService")
public class TableServiceImpl implements TableService{
	
	@Resource(name = "tableDao")
	private TableDao tableDao;
	
	private ConfigureInit configureInit;
	
	public void setConfigureInit(ConfigureInit configureInit) {
		this.configureInit = configureInit;
		tableDao.setConfigureInit(configureInit);
	}
	
	/** 
	 * 获取表的相关信息
	 * 
	 */
	public TableInfo getTableInfo(ResultInfo resultInfo) {
		// 获取表的主键
		List<String> keyColumn = getKeyColumn(resultInfo);
		if (keyColumn == null){
			resultInfo.setMsg("表的主键未找到");
			return null;
		}
		// 获取表字段
		List<ColumnInfo> columnsList = getColumns(resultInfo);
		if (columnsList == null){
			resultInfo.setMsg("表的字段未找到");
			return null;
		}
		TableInfo tableInfo = new TableInfo();
		tableInfo.setKeyColumn(keyColumn);
		tableInfo.setColumns(columnsList);
		tableInfo.initKeyColumns();
		return tableInfo;
	}

	// 获取表的主键
	public List<String> getKeyColumn(ResultInfo resultInfo) {
		resultInfo = tableDao.getKeyColumn();
		if (!resultInfo.isResult())
			return null;
		List<String> list = (List<String>) resultInfo.getObj();
		if (list == null || list.size() == 0) {
			resultInfo.setResult(false);
			resultInfo.setMsg("未发现主键信息");
			return null;
		}
		return list;
	}

	// 获取表字段
	public List<ColumnInfo> getColumns(ResultInfo resultInfo) {
		resultInfo = tableDao.getColumns();
		if (!resultInfo.isResult())
			return null;
		List<ColumnInfo> list = (List<ColumnInfo>) resultInfo.getObj();
		if (list == null || list.size() == 0) {
			resultInfo.setResult(false);
			resultInfo.setMsg("未发现表字段信息");
			return null;
		}
		return list;
	}
}
